<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收集 Promise 示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            width: 300px;
        }

        h1 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            color: #333;
        }

        .loading {
            font-size: 18px;
            color: #999;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>异步操作结果</h1>
        <button id="loadButton">加载数据</button>
        <div id="result" class="loading result">等待中...</div>
    </div>

    <script>
        // 定义 collectInto 函数
        const collectInto =
            (handler) =>
                (...args) =>
                    handler(args);

        // 使用 collectInto 来包装 Promise.all
        const Pall = collectInto(Promise.all.bind(Promise));

        // 模拟一些异步操作
        const promise1 = new Promise((resolve) =>
            setTimeout(() => resolve('数据 1 完成'), 1000)
        );
        const promise2 = new Promise((resolve) =>
            setTimeout(() => resolve('数据 2 完成'), 1500)
        );
        const promise3 = new Promise((resolve) =>
            setTimeout(() => resolve('数据 3 完成'), 2000)
        );

        // 绑定按钮点击事件
        document.getElementById('loadButton').addEventListener('click', () => {
            // 显示加载中的提示
            const resultElement = document.getElementById('result');
            resultElement.textContent = '加载中...';

            // 使用 Pall 处理多个 Promise
            Pall(promise1, promise2, promise3)
                .then((results) => {
                    // 显示所有 Promise 结果
                    resultElement.textContent = results.join(', ');
                })
                .catch((error) => {
                    // 捕获并显示错误
                    resultElement.textContent = `发生错误: ${error}`;
                });
        });
    </script>
</body>

</html>